<?php
namespace Home\Controller;
use Think\Controller;
class ContentController extends Controller {
    public function index() {    
      	$post_id = I('post.post_id', '');     
      	$post = D('post');
        $good = M('good');        
        $comment_good = M('comment_good');        
        $comment = D('comment');        
        $uid = session('uid');

        //查找文章
        $result['content'] = $post
        ->field(['td_post.post_id, td_post.post_title, td_post.post_type, td_post.post_content, td_post.post_text, td_post.post_image, td_post.post_createtime', 'COUNT(1) as comment_count'])
        ->where(array('td_post.post_id' => $post_id))
        ->join('left join td_comment c ON c.post_id = td_post.post_id')
        ->select();
        
        //查找评论
        $result['comments'] = $comment
        ->where(array('post_id' => $post_id))
        ->page(1, 5)
        ->order('comment_date desc')->select();
    
        // echo $comment->getLastSql(); exit;
        for($i = 0;  $i < count($result['comments']); $i++) {
            $result['comments'][$i]['comment_good_count'] = $comment_good->where(array('comment_id'=> $result['comments'][$i]['comment_id']))->count();
            $result['comments'][$i]['reply_content'] = $comment
            ->where(array('comment_reply_id' => $result['comments'][$i]['comment_reply_id']))->select();


            $result['comments'][$i]['isCommentGood'] =  'bad';
            for($j = 0;  $j < count($result['comments'][$i]['comment_goods']); $j++) {
                if($result['comments'][$i]['comment_goods'][$j]['uid'] == $uid) {
                    $result['comments'][$i]['isCommentGood'] =  'good';
                }
            }
        }

        //当前用户是否点赞
        $len = $good->where(array('post_id' => $post_id, 'uid' => $uid))->count();
        $result['goodCount'] = $good->where(array('post_id' => $post_id))->count();
        $result['isGood'] = $len > 0 ? 'good' : "bad";

      	if(empty($result['content'])) {
      		$result['status'] = false;      		
      	}else {
      		$result['status'] = true;
      	}

        $result['uid'] = $uid;
        $result['username'] = session('userName');
        $result['photo'] = session('photo');
        $result['content'][0]["post_content"] = html_entity_decode($result['content'][0]["post_content"]);
      	$this->ajaxReturn($result);
    }

    //分页评论列表
    public function getComment(){    
        $post_id = I('post.post_id', '');     
        $nowPage = I('post.nowPage', '');
        $count = I('post.count', '');
        $comment = M('comment');    

        $result['commentList'] = $comment->where(array('post_id' => $post_id))->order('comment_date desc')->page($nowPage, $count)->select();
        $this->ajaxReturn($result);
    }

    //评论点赞
    public function commentGood(){    
        $comment_id = I('post.comment_id', '');
        $comment_good = M('comment_good');
        $uid = session('uid');   
        if($uid == "")  {
            $result["type"] = null;
        }else {            
            $goodResult = $comment_good->where(array('comment_id' => $comment_id, 'uid' => $uid))->select();            
            if(count($goodResult) < 1) {         
                $comment_good->data(array('comment_id' => $comment_id, 'uid' => $uid))->add();  
                $result['type'] = 'good';
            }else {
                $comment_good->where(array('comment_good_id' => $goodResult[0]['comment_good_id']))->delete();
                $result['type'] = 'bad';
            }
        }
        $this->ajaxReturn($result);
    }

    //评论
    public function comment() {    
        $comment = M('comment');        
        $data['comment_text'] = I('post.text', '');
        $data['comment_username'] = I('post.username', '');
        $data['comment_author'] = I('post.author', '');
        $data['comment_photo'] = I('post.photo', '');
        $data['post_id'] = I('post.post_id', '');     
        $data['comment_uid'] = I('post.comment_uid', '');     
        $data['comment_reply_id'] = I('post.comment_reply_id', '');     
        $data['comment_date'] = date("Y-m-d H:i:s");
        $result['comment_id'] = $comment->data($data)->add();
        $this->ajaxReturn($result);
    }

    //评论
    public function delcomment() {    
        $comment = M('comment');         
        $comment_id = I('post.comment_id', '');     
        $result = $comment->where(array('comment_id' => $comment_id))->delete();
        $this->ajaxReturn($result);
    }
}


